<?php

/**
 * This is the model class for table "tbl_product".
 *
 * The followings are the available columns in table 'tbl_product':
 * @property integer $product_id
 * @property string $product_name
 * @property integer $product_quantity
 * @property integer $product_price
 * @property integer $product_price_sale
 * @property string $product_desc
 * @property string $product_reviews
 * @property integer $category_id
 * @property integer $brand_id
 * @property string $product_timecreate
 * @property string $product_timeupdate
 * @property string $product_image
 * @property string $product_image1
 * @property string $product_image2
 * @property string $product_image3
 * @property string $product_image4
 * @property string $product_image5
 * @property string $product_image6
 * @property string $product_image7
 * @property string $product_image8
 * @property string $product_image9
 * @property string $product_video
 * @property integer $product_view
 * @property string $url
 *
 * The followings are the available model relations:
 * @property Orderdetail[] $orderdetails
 * @property Price[] $prices
 * @property Category $category
 */
class Product extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_product';
	}

    /**
    *@return string product's name
    */
    public function getName($product_id){
        $product=Product::model()->findByPk($product_id);
        return $product['product_name'];    
    }
    
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('product_name, category_id, product_image', 'required','message'=>REQUIRED_NOT_NULL),
            array('product_image','url','message'=>IS_URL),
            array('url','unique','allowEmpty'=>true),
            array('url','url','allowEmpty'=>true),
			array('product_quantity, product_price, product_price_sale, category_id, brand_id, product_view', 'numerical', 'integerOnly'=>true),
			array('product_name', 'length', 'max'=>100),
			array('product_desc, product_reviews, product_timecreate, product_timeupdate, product_image1, product_image2, product_image3, product_image4, product_image5, product_image6, product_image7, product_image8, product_image9, product_video, url', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('product_id, product_name, product_quantity, product_price, product_price_sale, product_desc, product_reviews, category_id, brand_id, product_timecreate, product_timeupdate, product_image, product_image1, product_image2, product_image3, product_image4, product_image5, product_image6, product_image7, product_image8, product_image9, product_video, product_view, url', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'orderdetails' => array(self::HAS_MANY, 'Orderdetail', 'product_id'),
			'prices' => array(self::HAS_MANY, 'Price', 'product_id'),
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'product_id' => 'Mã',
			'product_name' => 'Tên sản phẩm',
			'product_quantity' => 'Số lượng',
			'product_price' => 'Giá',
			'product_price_sale' => 'Giá KM',
			'product_desc' => 'Mô tả',
			'product_reviews' => 'Đánh giá',
			'category_id' => 'Danh mục',
			'brand_id' => 'Thương hiệu',
			'product_timecreate' => 'Ngày tạo',
			'product_timeupdate' => 'Ngày cập nhật',
			'product_image' => 'Ảnh chính',
			'product_image1' => 'Ảnh 1',
			'product_image2' => 'Ảnh 2',
			'product_image3' => 'Ảnh 3',
			'product_image4' => 'Ảnh 4',
			'product_image5' => 'Ảnh 5',
			'product_image6' => 'Ảnh 6',
			'product_image7' => 'Ảnh 7',
			'product_image8' => 'Ảnh 8',
			'product_image9' => 'Ảnh 9',
			'product_video' => 'Video Url',
			'product_view' => 'Lượt xem',
			'url' => 'Link',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('product_id',$this->product_id);
		$criteria->compare('product_name',$this->product_name,true);
		$criteria->compare('product_quantity',$this->product_quantity);
		$criteria->compare('product_price',$this->product_price);
		$criteria->compare('product_price_sale',$this->product_price_sale);
		$criteria->compare('product_desc',$this->product_desc,true);
		$criteria->compare('product_reviews',$this->product_reviews,true);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('brand_id',$this->brand_id);
		$criteria->compare('product_timecreate',$this->product_timecreate,true);
		$criteria->compare('product_timeupdate',$this->product_timeupdate,true);
		$criteria->compare('product_image',$this->product_image,true);
		$criteria->compare('product_image1',$this->product_image1,true);
		$criteria->compare('product_image2',$this->product_image2,true);
		$criteria->compare('product_image3',$this->product_image3,true);
		$criteria->compare('product_image4',$this->product_image4,true);
		$criteria->compare('product_image5',$this->product_image5,true);
		$criteria->compare('product_image6',$this->product_image6,true);
		$criteria->compare('product_image7',$this->product_image7,true);
		$criteria->compare('product_image8',$this->product_image8,true);
		$criteria->compare('product_image9',$this->product_image9,true);
		$criteria->compare('product_video',$this->product_video,true);
		$criteria->compare('product_view',$this->product_view);
		$criteria->compare('url',$this->url,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Product the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
